home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / coffread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-02  |  58.0 KB  |  2,117 lines

  1. /* Read coff symbol tables and convert to internal format, for GDB.
  2.    Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
  3.    Copyright 1987, 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "symtab.h"
  23. #include "gdbtypes.h"
  24. #include "breakpoint.h"
  25. #include "bfd.h"
  26. #include "symfile.h"
  27. #include "objfiles.h"
  28. #include "buildsym.h"
  29. #include <obstack.h>
  30.  
  31. #include <string.h>
  32.  
  33. #include "coff/internal.h"    /* Internal format of COFF symbols in BFD */
  34. #include "libcoff.h"        /* FIXME secret internal data from BFD */
  35.  
  36. /* To be an sdb debug type, type must have at least a basic or primary
  37.    derived type.  Using this rather than checking against T_NULL is
  38.    said to prevent core dumps if we try to operate on Michael Bloom
  39.    dbx-in-coff file.  */
  40.  
  41. #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
  42.  
  43. /*
  44.  * Convert from an sdb register number to an internal gdb register number.
  45.  * This should be defined in tm.h, if REGISTER_NAMES is not set up
  46.  * to map one to one onto the sdb register numbers.
  47.  */
  48. #ifndef SDB_REG_TO_REGNUM
  49. # define SDB_REG_TO_REGNUM(value)     (value)
  50. #endif
  51.  
  52. /* Core address of start and end of text of current source file.
  53.    This comes from a ".text" symbol where x_nlinno > 0.  */
  54.  
  55. static CORE_ADDR cur_src_start_addr;
  56. static CORE_ADDR cur_src_end_addr;
  57.  
  58. /* Core address of the end of the first object file.  */
  59. static CORE_ADDR first_object_file_end;
  60.  
  61. /* The addresses of the symbol table stream and number of symbols
  62.    of the object file we are reading (as copied into core).  */
  63.  
  64. static FILE *nlist_stream_global;
  65. static int nlist_nsyms_global;
  66.  
  67. /* Vector of line number information.  */
  68.  
  69. static struct linetable *line_vector;
  70.  
  71. /* Index of next entry to go in line_vector_index.  */
  72.  
  73. static int line_vector_index;
  74.  
  75. /* Last line number recorded in the line vector.  */
  76.  
  77. static int prev_line_number;
  78.  
  79. /* Number of elements allocated for line_vector currently.  */
  80.  
  81. static int line_vector_length;
  82.  
  83. /* Pointers to scratch storage, used for reading raw symbols and auxents.  */
  84.  
  85. static char *temp_sym;
  86. static char *temp_aux;
  87.  
  88. /* Local variables that hold the shift and mask values for the
  89.    COFF file that we are currently reading.  These come back to us
  90.    from BFD, and are referenced by their macro names, as well as
  91.    internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
  92.    macros from ../internalcoff.h .  */
  93.  
  94. static unsigned    local_n_btmask;
  95. static unsigned    local_n_btshft;
  96. static unsigned    local_n_tmask;
  97. static unsigned    local_n_tshift;
  98.  
  99. #define    N_BTMASK    local_n_btmask
  100. #define    N_BTSHFT    local_n_btshft
  101. #define    N_TMASK        local_n_tmask
  102. #define    N_TSHIFT    local_n_tshift
  103.  
  104. /* Local variables that hold the sizes in the file of various COFF structures.
  105.    (We only need to know this to read them from the file -- BFD will then
  106.    translate the data in them, into `internal_xxx' structs in the right
  107.    byte order, alignment, etc.)  */
  108.  
  109. static unsigned    local_linesz;
  110. static unsigned    local_symesz;
  111. static unsigned    local_auxesz;
  112.  
  113.  
  114. /* Chain of typedefs of pointers to empty struct/union types.
  115.    They are chained thru the SYMBOL_VALUE_CHAIN.  */
  116.  
  117. static struct symbol *opaque_type_chain[HASHSIZE];
  118.  
  119. /* Record the symbols defined for each context in a list.
  120.    We don't create a struct block for the context until we
  121.    know how long to make it.  */
  122.  
  123. struct coff_pending
  124. {
  125.   struct coff_pending *next;
  126.   struct symbol *symbol;
  127. };
  128.  
  129. /* Here are the three lists that symbols are put on.  */
  130.  
  131. struct coff_pending *coff_file_symbols;    /* static at top level, and types */
  132.  
  133. struct coff_pending *coff_global_symbols;    /* global functions and variables */
  134.  
  135. struct coff_pending *coff_local_symbols;    /* everything local to lexical context */
  136.  
  137. /* List of unclosed lexical contexts
  138.    (that will become blocks, eventually).  */
  139.  
  140. struct coff_context_stack
  141. {
  142.   struct coff_context_stack *next;
  143.   struct coff_pending *locals;
  144.   struct pending_block *old_blocks;
  145.   struct symbol *name;
  146.   CORE_ADDR start_addr;
  147.   int depth;
  148. };
  149.  
  150. struct coff_context_stack *coff_context_stack;
  151.  
  152. /* Nonzero if within a function (so symbols should be local,
  153.    if nothing says specifically).  */
  154.  
  155. int within_function;
  156.  
  157. #if 0
  158. /* The type of the function we are currently reading in.  This is
  159.    used by define_symbol to record the type of arguments to a function. */
  160.  
  161. struct type *in_function_type;
  162. #endif
  163.  
  164. struct pending_block *pending_blocks;
  165.  
  166. /* Complaints about various problems in the file being read  */
  167.  
  168. struct complaint ef_complaint = 
  169.   {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
  170.  
  171. struct complaint bf_no_aux_complaint =
  172.   {"`.bf' symbol %d has no aux entry", 0, 0};
  173.  
  174. struct complaint ef_no_aux_complaint =
  175.   {"`.ef' symbol %d has no aux entry", 0, 0};
  176.  
  177. struct complaint lineno_complaint =
  178.   {"Line number pointer %d lower than start of line numbers", 0, 0};
  179.  
  180. struct complaint unexpected_type_complaint =
  181.   {"Unexpected type for symbol %s", 0, 0};
  182.  
  183. struct complaint bad_sclass_complaint =
  184.   {"Bad n_sclass for symbol %s", 0, 0};
  185.  
  186. /* Simplified internal version of coff symbol table information */
  187.  
  188. struct coff_symbol {
  189.   char *c_name;
  190.   int c_symnum;        /* symbol number of this entry */
  191.   int c_naux;        /* 0 if syment only, 1 if syment + auxent, etc */
  192.   long c_value;
  193.   int c_sclass;
  194.   int c_secnum;
  195.   unsigned int c_type;
  196. };
  197.  
  198. static struct type *
  199. coff_read_struct_type PARAMS ((int, int, int));
  200.  
  201. static struct type *
  202. decode_base_type PARAMS ((struct coff_symbol *, unsigned int,
  203.               union internal_auxent *));
  204.  
  205. static struct type *
  206. decode_type PARAMS ((struct coff_symbol *, unsigned int,
  207.              union internal_auxent *));
  208.  
  209. static struct type *
  210. decode_function_type PARAMS ((struct coff_symbol *, unsigned int,
  211.                   union internal_auxent *));
  212.  
  213. static struct type *
  214. coff_read_enum_type PARAMS ((int, int, int));
  215.  
  216. static struct blockvector *
  217. make_blockvector PARAMS ((struct objfile *));
  218.  
  219. static struct symbol *
  220. process_coff_symbol PARAMS ((struct coff_symbol *, union internal_auxent *,
  221.                  struct objfile *));
  222.  
  223. static void
  224. patch_opaque_types PARAMS ((struct symtab *));
  225.  
  226. static void
  227. patch_type PARAMS ((struct type *, struct type *));
  228.  
  229. static void
  230. enter_linenos PARAMS ((long, int, int));
  231.  
  232. static int
  233. init_lineno PARAMS ((int, long, int));
  234.  
  235. static char *
  236. getfilename PARAMS ((union internal_auxent *));
  237.  
  238. static char *
  239. getsymname PARAMS ((struct internal_syment *));
  240.  
  241. static void
  242. free_stringtab PARAMS ((void));
  243.  
  244. static int
  245. init_stringtab PARAMS ((int, long));
  246.  
  247. static void
  248. read_one_sym PARAMS ((struct coff_symbol *, struct internal_syment *,
  249.               union internal_auxent *));
  250.  
  251. static void
  252. read_coff_symtab PARAMS ((int, int, struct objfile *));
  253.  
  254. static void
  255. find_linenos PARAMS ((bfd *, sec_ptr, PTR));
  256.  
  257. static void
  258. coff_symfile_init PARAMS ((struct objfile *));
  259.  
  260. static void
  261. coff_new_init PARAMS ((struct objfile *));
  262.  
  263. static void
  264. coff_symfile_read PARAMS ((struct objfile *, CORE_ADDR, int));
  265.  
  266. static void
  267. coff_symfile_finish PARAMS ((struct objfile *));
  268.  
  269. static void
  270. record_minimal_symbol PARAMS ((char *, CORE_ADDR));
  271.  
  272. static void
  273. coff_end_symtab PARAMS ((struct objfile *));
  274.  
  275. static void
  276. complete_symtab PARAMS ((char *, CORE_ADDR, unsigned int));
  277.  
  278. static void
  279. coff_start_symtab PARAMS ((void));
  280.  
  281. static void
  282. coff_record_line PARAMS ((int, CORE_ADDR));
  283.  
  284. static void
  285. coff_finish_block PARAMS ((struct symbol *, struct coff_pending **,
  286.                struct pending_block *, CORE_ADDR, CORE_ADDR,
  287.                struct objfile *));
  288.  
  289. static void
  290. coff_add_symbol_to_list PARAMS ((struct symbol *, struct coff_pending **));
  291.  
  292. static struct type *
  293. coff_alloc_type PARAMS ((int));
  294.  
  295. static struct type **
  296. coff_lookup_type PARAMS ((int));
  297.  
  298.  
  299. /* Look up a coff type-number index.  Return the address of the slot
  300.    where the type for that index is stored.
  301.    The type-number is in INDEX. 
  302.  
  303.    This can be used for finding the type associated with that index
  304.    or for associating a new type with the index.  */
  305.  
  306. static struct type **
  307. coff_lookup_type (index)
  308.      register int index;
  309. {
  310.   if (index >= type_vector_length)
  311.     {
  312.       int old_vector_length = type_vector_length;
  313.  
  314.       type_vector_length *= 2;
  315.       if (type_vector_length < index) {
  316.     type_vector_length = index * 2;
  317.       }
  318.       type_vector = (struct type **)
  319.     xrealloc ((char *) type_vector,
  320.           type_vector_length * sizeof (struct type *));
  321.       bzero (&type_vector[old_vector_length],
  322.          (type_vector_length - old_vector_length) * sizeof(struct type *));
  323.     }
  324.   return &type_vector[index];
  325. }
  326.  
  327. /* Make sure there is a type allocated for type number index
  328.    and return the type object.
  329.    This can create an empty (zeroed) type object.  */
  330.  
  331. static struct type *
  332. coff_alloc_type (index)
  333.      int index;
  334. {
  335.   register struct type **type_addr = coff_lookup_type (index);
  336.   register struct type *type = *type_addr;
  337.  
  338.   /* If we are referring to a type not known at all yet,
  339.      allocate an empty type for it.
  340.      We will fill it in later if we find out how.  */
  341.   if (type == NULL)
  342.     {
  343.       type = alloc_type (current_objfile);
  344.       *type_addr = type;
  345.     }
  346.   return type;
  347. }
  348.  
  349. /* maintain the lists of symbols and blocks */
  350.  
  351. /* Add a symbol to one of the lists of symbols.  */
  352. static void
  353. coff_add_symbol_to_list (symbol, listhead)
  354.      struct symbol *symbol;
  355.      struct coff_pending **listhead;
  356. {
  357.   register struct coff_pending *link
  358.     = (struct coff_pending *) xmalloc (sizeof (struct coff_pending));
  359.  
  360.   link->next = *listhead;
  361.   link->symbol = symbol;
  362.   *listhead = link;
  363. }
  364.  
  365. /* Take one of the lists of symbols and make a block from it.
  366.    Put the block on the list of pending blocks.  */
  367.  
  368. static void
  369. coff_finish_block (symbol, listhead, old_blocks, start, end, objfile)
  370.      struct symbol *symbol;
  371.      struct coff_pending **listhead;
  372.      struct pending_block *old_blocks;
  373.      CORE_ADDR start, end;
  374.      struct objfile *objfile;
  375. {
  376.   register struct coff_pending *next, *next1;
  377.   register struct block *block;
  378.   register struct pending_block *pblock;
  379.   struct pending_block *opblock;
  380.   register int i;
  381.  
  382.   /* Count the length of the list of symbols.  */
  383.  
  384.   for (next = *listhead, i = 0; next; next = next->next, i++);
  385.  
  386.   block = (struct block *)
  387.         obstack_alloc (&objfile->symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
  388.  
  389.   /* Copy the symbols into the block.  */
  390.  
  391.   BLOCK_NSYMS (block) = i;
  392.   for (next = *listhead; next; next = next->next)
  393.     BLOCK_SYM (block, --i) = next->symbol;
  394.  
  395.   BLOCK_START (block) = start;
  396.   BLOCK_END (block) = end;
  397.   BLOCK_SUPERBLOCK (block) = 0;    /* Filled in when containing block is made */
  398.  
  399.   /* Put the block in as the value of the symbol that names it.  */
  400.  
  401.   if (symbol)
  402.     {
  403.       SYMBOL_BLOCK_VALUE (symbol) = block;
  404.       BLOCK_FUNCTION (block) = symbol;
  405.     }
  406.   else
  407.     BLOCK_FUNCTION (block) = 0;
  408.  
  409.   /* Now free the links of the list, and empty the list.  */
  410.  
  411.   for (next = *listhead; next; next = next1)
  412.     {
  413.       next1 = next->next;
  414.       free ((PTR)next);
  415.     }
  416.   *listhead = 0;
  417.  
  418.   /* Install this block as the superblock
  419.      of all blocks made since the start of this scope
  420.      that don't have superblocks yet.  */
  421.  
  422.   opblock = 0;
  423.   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
  424.     {
  425.       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
  426.     BLOCK_SUPERBLOCK (pblock->block) = block;
  427.       opblock = pblock;
  428.     }
  429.  
  430.   /* Record this block on the list of all blocks in the file.
  431.      Put it after opblock, or at the beginning if opblock is 0.
  432.      This puts the block in the list after all its subblocks.  */
  433.  
  434.   pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
  435.   pblock->block = block;
  436.   if (opblock)
  437.     {
  438.       pblock->next = opblock->next;
  439.       opblock->next = pblock;
  440.     }
  441.   else
  442.     {
  443.       pblock->next = pending_blocks;
  444.       pending_blocks = pblock;
  445.     }
  446. }
  447.  
  448. static struct blockvector *
  449. make_blockvector (objfile)
  450.      struct objfile *objfile;
  451. {
  452.   register struct pending_block *next, *next1;
  453.   register struct blockvector *blockvector;
  454.   register int i;
  455.  
  456.   /* Count the length of the list of blocks.  */
  457.  
  458.   for (next = pending_blocks, i = 0; next; next = next->next, i++);
  459.  
  460.   blockvector = (struct blockvector *)
  461.           obstack_alloc (&objfile->symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
  462.  
  463.   /* Copy the blocks into the blockvector.
  464.      This is done in reverse order, which happens to put
  465.      the blocks into the proper order (ascending starting address).
  466.      coff_finish_block has hair to insert each block into the list
  467.      after its subblocks in order to make sure this is true.  */
  468.  
  469.   BLOCKVECTOR_NBLOCKS (blockvector) = i;
  470.   for (next = pending_blocks; next; next = next->next)
  471.     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  472.  
  473.   /* Now free the links of the list, and empty the list.  */
  474.  
  475.   for (next = pending_blocks; next; next = next1)
  476.     {
  477.       next1 = next->next;
  478.       free ((PTR)next);
  479.     }
  480.   pending_blocks = 0;
  481.  
  482.   return blockvector;
  483. }
  484.  
  485. /* Manage the vector of line numbers.  */
  486.  
  487. static void
  488. coff_record_line (line, pc)
  489.      int line;
  490.      CORE_ADDR pc;
  491. {
  492.   struct linetable_entry *e;
  493.   /* Make sure line vector is big enough.  */
  494.  
  495.   if (line_vector_index + 2 >= line_vector_length)
  496.     {
  497.       line_vector_length *= 2;
  498.       line_vector = (struct linetable *)
  499.     xrealloc ((char *) line_vector, sizeof (struct linetable)
  500.           + (line_vector_length
  501.              * sizeof (struct linetable_entry)));
  502.     }
  503.  
  504.   e = line_vector->item + line_vector_index++;
  505.   e->line = line; e->pc = pc;
  506. }
  507.  
  508. /* Start a new symtab for a new source file.
  509.    This is called when a COFF ".file" symbol is seen;
  510.    it indicates the start of data for one original source file.  */
  511.  
  512. static void
  513. coff_start_symtab ()
  514. {
  515.   coff_file_symbols = 0;
  516.   coff_global_symbols = 0;
  517.   coff_context_stack = 0;
  518.   within_function = 0;
  519.   last_source_file = 0;
  520.  
  521.   /* Initialize the source file line number information for this file.  */
  522.  
  523.   if (line_vector)        /* Unlikely, but maybe possible? */
  524.     free ((PTR)line_vector);
  525.   line_vector_index = 0;
  526.   line_vector_length = 1000;
  527.   prev_line_number = -2;    /* Force first line number to be explicit */
  528.   line_vector = (struct linetable *)
  529.     xmalloc (sizeof (struct linetable)
  530.          + line_vector_length * sizeof (struct linetable_entry));
  531. }
  532.  
  533. /* Save the vital information from when starting to read a file,
  534.    for use when closing off the current file.
  535.    NAME is the file name the symbols came from, START_ADDR is the first
  536.    text address for the file, and SIZE is the number of bytes of text.  */
  537.  
  538. static void
  539. complete_symtab (name, start_addr, size)
  540.     char *name;
  541.     CORE_ADDR start_addr;
  542.     unsigned int size;
  543. {
  544.   last_source_file = savestring (name, strlen (name));
  545.   cur_src_start_addr = start_addr;
  546.   cur_src_end_addr = start_addr + size;
  547.  
  548.   if (current_objfile -> ei.entry_point >= cur_src_start_addr &&
  549.       current_objfile -> ei.entry_point <  cur_src_end_addr)
  550.     {
  551.       current_objfile -> ei.entry_file_lowpc = cur_src_start_addr;
  552.       current_objfile -> ei.entry_file_highpc = cur_src_end_addr;
  553.     }
  554. }
  555.  
  556. /* Finish the symbol definitions for one main source file,
  557.    close off all the lexical contexts for that file
  558.    (creating struct block's for them), then make the
  559.    struct symtab for that file and put it in the list of all such. */
  560.  
  561. static void
  562. coff_end_symtab (objfile)
  563.      struct objfile *objfile;
  564. {
  565.   register struct symtab *symtab;
  566.   register struct coff_context_stack *cstk;
  567.   register struct blockvector *blockvector;
  568.   register struct linetable *lv;
  569.  
  570.   /* Finish the lexical context of the last function in the file.  */
  571.  
  572.   if (coff_context_stack)
  573.     {
  574.       cstk = coff_context_stack;
  575.       coff_context_stack = 0;
  576.       /* Make a block for the local symbols within.  */
  577.       coff_finish_block (cstk->name, &coff_local_symbols, cstk->old_blocks,
  578.             cstk->start_addr, cur_src_end_addr, objfile);
  579.       free ((PTR)cstk);
  580.     }
  581.  
  582.   /* Ignore a file that has no functions with real debugging info.  */
  583.   if (pending_blocks == 0 && coff_file_symbols == 0 && coff_global_symbols == 0)
  584.     {
  585.       free ((PTR)line_vector);
  586.       line_vector = 0;
  587.       line_vector_length = -1;
  588.       last_source_file = 0;
  589.       return;
  590.     }
  591.  
  592.   /* Create the two top-level blocks for this file (STATIC_BLOCK and
  593.      GLOBAL_BLOCK).  */
  594.   coff_finish_block (0, &coff_file_symbols, 0, cur_src_start_addr, cur_src_end_addr, objfile);
  595.   coff_finish_block (0, &coff_global_symbols, 0, cur_src_start_addr, cur_src_end_addr, objfile);
  596.  
  597.   /* Create the blockvector that points to all the file's blocks.  */
  598.   blockvector = make_blockvector (objfile);
  599.  
  600.   /* Now create the symtab object for this source file.  */
  601.   symtab = allocate_symtab (last_source_file, objfile);
  602.  
  603.   /* Fill in its components.  */
  604.   symtab->blockvector = blockvector;
  605.   symtab->free_code = free_linetable;
  606.   symtab->free_ptr = 0;
  607.   symtab->filename = last_source_file;
  608.   symtab->dirname = NULL;
  609.   lv = line_vector;
  610.   lv->nitems = line_vector_index;
  611.   symtab->linetable = (struct linetable *)
  612.     xrealloc ((char *) lv, (sizeof (struct linetable)
  613.            + lv->nitems * sizeof (struct linetable_entry)));
  614.  
  615.   free_named_symtabs (symtab->filename);
  616.  
  617.   /* Reinitialize for beginning of new file. */
  618.   line_vector = 0;
  619.   line_vector_length = -1;
  620.   last_source_file = 0;
  621. }
  622.  
  623. static void
  624. record_minimal_symbol (name, address)
  625.      char *name;
  626.      CORE_ADDR address;
  627. {
  628.   /* We don't want TDESC entry points in the minimal symbol table */
  629.   if (name[0] == '@') return;
  630.  
  631.   /* mst_text isn't true, but apparently COFF doesn't tell us what it really
  632.      is, so this guess is more useful than mst_unknown.  */
  633.   prim_record_minimal_symbol (savestring (name, strlen (name)),
  634.                  address,
  635.                  (int)mst_text);
  636. }
  637.  
  638. /* coff_symfile_init ()
  639.    is the coff-specific initialization routine for reading symbols.
  640.    It is passed a struct objfile which contains, among other things,
  641.    the BFD for the file whose symbols are being read, and a slot for
  642.    a pointer to "private data" which we fill with cookies and other
  643.    treats for coff_symfile_read ().
  644.  
  645.    We will only be called if this is a COFF or COFF-like file.
  646.    BFD handles figuring out the format of the file, and code in symtab.c
  647.    uses BFD's determination to vector to us.
  648.  
  649.    The ultimate result is a new symtab (or, FIXME, eventually a psymtab).  */
  650.  
  651. struct coff_symfile_info {
  652.   file_ptr min_lineno_offset;        /* Where in file lowest line#s are */
  653.   file_ptr max_lineno_offset;        /* 1+last byte of line#s in file */
  654. };
  655.  
  656. static int text_bfd_scnum;
  657.  
  658. static void
  659. coff_symfile_init (objfile)
  660.      struct objfile *objfile;
  661. {
  662.   asection    *section;
  663.   bfd *abfd = objfile->obfd;
  664.  
  665.   /* Allocate struct to keep track of the symfile */
  666.   objfile -> sym_private = xmmalloc (objfile -> md,
  667.                      sizeof (struct coff_symfile_info));
  668.  
  669.   init_entry_point_info (objfile);
  670.  
  671.   /* Save the section number for the text section */
  672.   section = bfd_get_section_by_name(abfd,".text");
  673.   if (section)
  674.     text_bfd_scnum = section->index;
  675.   else
  676.     text_bfd_scnum = -1; 
  677. }
  678.  
  679. /* This function is called for every section; it finds the outer limits
  680.    of the line table (minimum and maximum file offset) so that the
  681.    mainline code can read the whole thing for efficiency.  */
  682.  
  683. /* ARGSUSED */
  684. static void
  685. find_linenos (abfd, asect, vpinfo)
  686.      bfd *abfd;
  687.      sec_ptr asect;
  688.      PTR vpinfo;
  689. {
  690.   struct coff_symfile_info *info;
  691.   int size, count;
  692.   file_ptr offset, maxoff;
  693.  
  694. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  695.   count = asect->lineno_count;
  696. /* End of warning */
  697.  
  698.   if (count == 0)
  699.     return;
  700.   size = count * local_linesz;
  701.  
  702.   info = (struct coff_symfile_info *)vpinfo;
  703. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  704.   offset = asect->line_filepos;
  705. /* End of warning */
  706.  
  707.   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
  708.     info->min_lineno_offset = offset;
  709.  
  710.   maxoff = offset + size;
  711.   if (maxoff > info->max_lineno_offset)
  712.     info->max_lineno_offset = maxoff;
  713. }
  714.  
  715.  
  716. /* The BFD for this file -- only good while we're actively reading
  717.    symbols into a psymtab or a symtab.  */
  718.  
  719. static bfd *symfile_bfd;
  720.  
  721. /* Read a symbol file, after initialization by coff_symfile_init.  */
  722. /* FIXME!  Addr and Mainline are not used yet -- this will not work for
  723.    shared libraries or add_file!  */
  724.  
  725. /* ARGSUSED */
  726. static void
  727. coff_symfile_read (objfile, addr, mainline)
  728.      struct objfile *objfile;
  729.      CORE_ADDR addr;
  730.      int mainline;
  731. {
  732.   struct coff_symfile_info *info;
  733.   bfd *abfd = objfile->obfd;
  734.   coff_data_type *cdata = coff_data (abfd);
  735.   char *name = bfd_get_filename (abfd);
  736.   int desc;
  737.   register int val;
  738.   int num_symbols;
  739.   int symtab_offset;
  740.   int stringtab_offset;
  741.   struct objfile *objfile2;
  742.   struct symtab *s;
  743.  
  744.   info = (struct coff_symfile_info *) objfile -> sym_private;
  745.   symfile_bfd = abfd;            /* Kludge for swap routines */
  746.  
  747. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  748.    desc = fileno ((FILE *)(abfd->iostream));    /* File descriptor */
  749.    num_symbols = bfd_get_symcount (abfd);    /* How many syms */
  750.    symtab_offset = cdata->sym_filepos;        /* Symbol table file offset */
  751.    stringtab_offset = symtab_offset +        /* String table file offset */
  752.               num_symbols * cdata->local_symesz;
  753.  
  754.   /* Set a few file-statics that give us specific information about
  755.      the particular COFF file format we're reading.  */
  756.   local_linesz   = cdata->local_linesz;
  757.   local_n_btmask = cdata->local_n_btmask;
  758.   local_n_btshft = cdata->local_n_btshft;
  759.   local_n_tmask  = cdata->local_n_tmask;
  760.   local_n_tshift = cdata->local_n_tshift;
  761.   local_linesz   = cdata->local_linesz;
  762.   local_symesz   = cdata->local_symesz;
  763.   local_auxesz   = cdata->local_auxesz;
  764.  
  765.   /* Allocate space for raw symbol and aux entries, based on their
  766.      space requirements as reported by BFD.  */
  767.   temp_sym = (char *) xmalloc
  768.      (cdata->local_symesz + cdata->local_auxesz);
  769.   temp_aux = temp_sym + cdata->local_symesz;
  770.   make_cleanup (free_current_contents, &temp_sym);
  771. /* End of warning */
  772.  
  773.   /* Read the line number table, all at once.  */
  774.   info->min_lineno_offset = 0;
  775.   info->max_lineno_offset = 0;
  776.   bfd_map_over_sections (abfd, find_linenos, (PTR)info);
  777.  
  778.   val = init_lineno (desc, info->min_lineno_offset, 
  779.              info->max_lineno_offset - info->min_lineno_offset);
  780.   if (val < 0)
  781.     error ("\"%s\": error reading line numbers\n", name);
  782.  
  783.   /* Now read the string table, all at once.  */
  784.  
  785.   val = init_stringtab (desc, stringtab_offset);
  786.   if (val < 0)
  787.     error ("\"%s\": can't get string table", name);
  788.   make_cleanup (free_stringtab, 0);
  789.  
  790.   /* Position to read the symbol table.  Do not read it all at once. */
  791.   val = lseek (desc, (long)symtab_offset, 0);
  792.   if (val < 0)
  793.     perror_with_name (name);
  794.  
  795.   init_minimal_symbol_collection ();
  796.   make_cleanup (discard_minimal_symbols, 0);
  797.  
  798.   /* Now that the executable file is positioned at symbol table,
  799.      process it and define symbols accordingly.  */
  800.  
  801.   read_coff_symtab (desc, num_symbols, objfile);
  802.  
  803.   ALL_SYMTABS (objfile2, s)
  804.     patch_opaque_types (s);
  805.  
  806.   /* Sort symbols alphabetically within each block.  */
  807.  
  808.   sort_all_symtab_syms ();
  809.  
  810.   /* Install any minimal symbols that have been collected as the current
  811.      minimal symbols for this objfile. */
  812.  
  813.   install_minimal_symbols (objfile);
  814. }
  815.  
  816. static void
  817. coff_new_init (ignore)
  818.      struct objfile *ignore;
  819. {
  820.     /* Nothin' to do */
  821. }
  822.  
  823. /* Perform any local cleanups required when we are done with a particular
  824.    objfile.  I.E, we are in the process of discarding all symbol information
  825.    for an objfile, freeing up all memory held for it, and unlinking the
  826.    objfile struct from the global list of known objfiles. */
  827.  
  828. static void
  829. coff_symfile_finish (objfile)
  830.      struct objfile *objfile;
  831. {
  832.   if (objfile -> sym_private != NULL)
  833.     {
  834.       mfree (objfile -> md, objfile -> sym_private);
  835.     }
  836. }
  837.  
  838.  
  839. /* Given pointers to a symbol table in coff style exec file,
  840.    analyze them and create struct symtab's describing the symbols.
  841.    NSYMS is the number of symbols in the symbol table.
  842.    We read them one at a time using read_one_sym ().  */
  843.  
  844. static void
  845. read_coff_symtab (desc, nsyms, objfile)
  846.      int desc;
  847.      int nsyms;
  848.      struct objfile *objfile;
  849. {
  850.   int newfd;            /* Avoid multiple closes on same desc */
  851.   FILE *stream; 
  852.   register struct coff_context_stack *new;
  853.   struct coff_symbol coff_symbol;
  854.   register struct coff_symbol *cs = &coff_symbol;
  855.   static struct internal_syment main_sym;
  856.   static union internal_auxent main_aux;
  857.   struct coff_symbol fcn_cs_saved;
  858.   static struct internal_syment fcn_sym_saved;
  859.   static union internal_auxent fcn_aux_saved;
  860.  
  861.   /* A .file is open.  */
  862.   int in_source_file = 0;
  863.   int num_object_files = 0;
  864.   int next_file_symnum = -1;
  865.  
  866.   /* Name of the current file.  */
  867.   char *filestring = "";
  868.   int depth = 0;
  869.   int fcn_first_line = 0;
  870.   int fcn_last_line = 0;
  871.   int fcn_start_addr = 0;
  872.   long fcn_line_ptr = 0;
  873.   struct cleanup *old_chain;
  874.  
  875.  
  876.   newfd = dup (desc);
  877.   if (newfd == -1)
  878.     fatal ("Too many open files");
  879.   stream = fdopen (newfd, "r");
  880.  
  881.   /* These cleanups will be discarded below if we succeed.  */
  882.   old_chain = make_cleanup (free_objfile, objfile);
  883.   make_cleanup (fclose, stream);
  884.  
  885.   current_objfile = objfile;
  886.   nlist_stream_global = stream;
  887.   nlist_nsyms_global = nsyms;
  888.   last_source_file = 0;
  889.   bzero (opaque_type_chain, sizeof opaque_type_chain);
  890.  
  891.   if (type_vector)            /* Get rid of previous one */
  892.     free ((PTR)type_vector);
  893.   type_vector_length = 160;
  894.   type_vector = (struct type **)
  895.         xmalloc (type_vector_length * sizeof (struct type *));
  896.   bzero (type_vector, type_vector_length * sizeof (struct type *));
  897.  
  898.   coff_start_symtab ();
  899.  
  900.   symnum = 0;
  901.   while (symnum < nsyms)
  902.     {
  903.       QUIT;            /* Make this command interruptable.  */
  904.       read_one_sym (cs, &main_sym, &main_aux);
  905.  
  906. #ifdef SEM
  907.       temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
  908.                      cs->c_name[2] << 8 | cs->c_name[3];
  909.       if (int_sem_val == temp_sem_val)
  910.         last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
  911. #endif
  912.  
  913.       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
  914.     {
  915.       if (last_source_file)
  916.         coff_end_symtab (objfile);
  917.  
  918.       coff_start_symtab ();
  919.       complete_symtab ("_globals_", 0, first_object_file_end);
  920.       /* done with all files, everything from here on out is globals */
  921.     }
  922.  
  923.       /* Special case for file with type declarations only, no text.  */
  924.       if (!last_source_file && SDB_TYPE (cs->c_type)
  925.       && cs->c_secnum == N_DEBUG)
  926.     complete_symtab (filestring, 0, 0);
  927.  
  928.       /* Typedefs should not be treated as symbol definitions.  */
  929.       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
  930.     {
  931.       /* record as a minimal symbol.  if we get '.bf' next,
  932.        * then we undo this step
  933.        */
  934.       record_minimal_symbol (cs->c_name, cs->c_value);
  935.  
  936.       fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
  937.       fcn_start_addr = cs->c_value;
  938.       fcn_cs_saved = *cs;
  939.       fcn_sym_saved = main_sym;
  940.       fcn_aux_saved = main_aux;
  941.       continue;
  942.     }
  943.  
  944.       switch (cs->c_sclass)
  945.     {
  946.       case C_EFCN:
  947.       case C_EXTDEF:
  948.       case C_ULABEL:
  949.       case C_USTATIC:
  950.       case C_LINE:
  951.       case C_ALIAS:
  952.       case C_HIDDEN:
  953.         complain (&bad_sclass_complaint, cs->c_name);
  954.         break;
  955.  
  956.       case C_FILE:
  957.         /*
  958.          * c_value field contains symnum of next .file entry in table
  959.          * or symnum of first global after last .file.
  960.          */
  961.         next_file_symnum = cs->c_value;
  962.         filestring = getfilename (&main_aux);
  963.         /*
  964.          * Complete symbol table for last object file
  965.          * containing debugging information.
  966.          */
  967.         if (last_source_file)
  968.           {
  969.         coff_end_symtab (objfile);
  970.         coff_start_symtab ();
  971.           }
  972.         in_source_file = 1;
  973.         break;
  974.  
  975.           case C_STAT:
  976.         if (cs->c_name[0] == '.') {
  977.             if (strcmp (cs->c_name, ".text") == 0) {
  978.                 /* FIXME:  don't wire in ".text" as section name
  979.                        or symbol name! */
  980.                 if (++num_object_files == 1) {
  981.                     /* last address of startup file */
  982.                     first_object_file_end = cs->c_value +
  983.                         main_aux.x_scn.x_scnlen;
  984.                 }
  985.                 /* Check for in_source_file deals with case of
  986.                    a file with debugging symbols
  987.                    followed by a later file with no symbols.  */
  988.                 if (in_source_file)
  989.                   complete_symtab (filestring, cs->c_value,
  990.                            main_aux.x_scn.x_scnlen);
  991.                 in_source_file = 0;
  992.             }
  993.             /* flush rest of '.' symbols */
  994.             break;
  995.         }
  996.         else if (!SDB_TYPE (cs->c_type)
  997.              && cs->c_name[0] == 'L'
  998.              && (strncmp (cs->c_name, "LI%", 3) == 0
  999.              || strncmp (cs->c_name, "LF%", 3) == 0
  1000.              || strncmp (cs->c_name,"LC%",3) == 0
  1001.              || strncmp (cs->c_name,"LP%",3) == 0
  1002.              || strncmp (cs->c_name,"LPB%",4) == 0
  1003.              || strncmp (cs->c_name,"LBB%",4) == 0
  1004.              || strncmp (cs->c_name,"LBE%",4) == 0
  1005.              || strncmp (cs->c_name,"LPBX%",5) == 0))
  1006.           /* At least on a 3b1, gcc generates swbeg and string labels
  1007.          that look like this.  Ignore them.  */
  1008.           break;
  1009.         /* fall in for static symbols that don't start with '.' */
  1010.       case C_EXT:
  1011.         if (!SDB_TYPE (cs->c_type)) {
  1012.         /* FIXME: This is BOGUS Will Robinson! 
  1013.          Coff should provide the SEC_CODE flag for executable sections,
  1014.          then if we could look up sections by section number we
  1015.            could see if the flags indicate SEC_CODE.  If so, then
  1016.          record this symbol as a function in the minimal symbol table.
  1017.         But why are absolute syms recorded as functions, anyway?  */
  1018.             if (cs->c_secnum <= text_bfd_scnum+1) {/* text or abs */
  1019.                 record_minimal_symbol (cs->c_name, cs->c_value);
  1020.                 break;
  1021.             } else {
  1022.                 cs->c_type = T_INT;
  1023.             }
  1024.         }
  1025.         (void) process_coff_symbol (cs, &main_aux, objfile);
  1026.         break;
  1027.  
  1028.       case C_FCN:
  1029.         if (strcmp (cs->c_name, ".bf") == 0)
  1030.           {
  1031.         within_function = 1;
  1032.  
  1033.         /* value contains address of first non-init type code */
  1034.         /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
  1035.                 contains line number of '{' } */
  1036.         if (cs->c_naux != 1)
  1037.           complain (&bf_no_aux_complaint, (char *) cs->c_symnum);
  1038.         fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  1039.  
  1040.         new = (struct coff_context_stack *)
  1041.           xmalloc (sizeof (struct coff_context_stack));
  1042.         new->depth = depth = 0;
  1043.         new->next = 0;
  1044.         coff_context_stack = new;
  1045.         new->locals = 0;
  1046.         new->old_blocks = pending_blocks;
  1047.         new->start_addr = fcn_start_addr;
  1048.         fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
  1049.         new->name = process_coff_symbol (&fcn_cs_saved,
  1050.                          &fcn_aux_saved, objfile);
  1051.           }
  1052.         else if (strcmp (cs->c_name, ".ef") == 0)
  1053.           {
  1054.               /* the value of .ef is the address of epilogue code;
  1055.                * not useful for gdb
  1056.                */
  1057.         /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
  1058.                 contains number of lines to '}' */
  1059.         new = coff_context_stack;
  1060.         if (new == 0)
  1061.           {
  1062.             complain (&ef_complaint, (char *) cs->c_symnum);
  1063.             within_function = 0;
  1064.             break;
  1065.           }
  1066.         if (cs->c_naux != 1) {
  1067.           complain (&ef_no_aux_complaint, (char *) cs->c_symnum);
  1068.           fcn_last_line = 0x7FFFFFFF;
  1069.         } else {
  1070.           fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  1071.         }
  1072.         enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
  1073.  
  1074.         coff_finish_block (new->name, &coff_local_symbols, new->old_blocks,
  1075.                   new->start_addr,
  1076. #if defined (FUNCTION_EPILOGUE_SIZE)
  1077.                   /* This macro should be defined only on
  1078.                  machines where the
  1079.                  fcn_aux_saved.x_sym.x_misc.x_fsize
  1080.                  field is always zero.
  1081.                  So use the .bf record information that
  1082.                  points to the epilogue and add the size
  1083.                  of the epilogue.  */
  1084.                   cs->c_value + FUNCTION_EPILOGUE_SIZE,
  1085. #else
  1086.                   fcn_cs_saved.c_value +
  1087.                       fcn_aux_saved.x_sym.x_misc.x_fsize,
  1088. #endif
  1089.                   objfile
  1090.                   );
  1091.         coff_context_stack = 0;
  1092.         within_function = 0;
  1093.         free ((PTR)new);
  1094.           }
  1095.         break;
  1096.  
  1097.       case C_BLOCK:
  1098.         if (strcmp (cs->c_name, ".bb") == 0)
  1099.           {
  1100.         new = (struct coff_context_stack *)
  1101.                 xmalloc (sizeof (struct coff_context_stack));
  1102.         depth++;
  1103.         new->depth = depth;
  1104.         new->next = coff_context_stack;
  1105.         coff_context_stack = new;
  1106.         new->locals = coff_local_symbols;
  1107.         new->old_blocks = pending_blocks;
  1108.         new->start_addr = cs->c_value;
  1109.         new->name = 0;
  1110.         coff_local_symbols = 0;
  1111.           }
  1112.         else if (strcmp (cs->c_name, ".eb") == 0)
  1113.           {
  1114.         new = coff_context_stack;
  1115.         if (new == 0 || depth != new->depth)
  1116.           error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
  1117.              symnum);
  1118.         if (coff_local_symbols && coff_context_stack->next)
  1119.           {
  1120.             /* Make a block for the local symbols within.  */
  1121.             coff_finish_block (0, &coff_local_symbols, new->old_blocks,
  1122.                   new->start_addr, cs->c_value, objfile);
  1123.           }
  1124.         depth--;
  1125.         coff_local_symbols = new->locals;
  1126.         coff_context_stack = new->next;
  1127.         free ((PTR)new);
  1128.           }
  1129.         break;
  1130.  
  1131.       default:
  1132.         (void) process_coff_symbol (cs, &main_aux, objfile);
  1133.         break;
  1134.     }
  1135.     }
  1136.  
  1137.   if (last_source_file)
  1138.     coff_end_symtab (objfile);
  1139.   fclose (stream);
  1140.   discard_cleanups (old_chain);
  1141.   current_objfile = NULL;
  1142. }
  1143.  
  1144. /* Routines for reading headers and symbols from executable.  */
  1145.  
  1146. #ifdef FIXME
  1147. /* Move these XXXMAGIC symbol defns into BFD!  */
  1148.  
  1149. /* Read COFF file header, check magic number,
  1150.    and return number of symbols. */
  1151. read_file_hdr (chan, file_hdr)
  1152.     int chan;
  1153.     FILHDR *file_hdr;
  1154. {
  1155.   lseek (chan, 0L, 0);
  1156.   if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
  1157.     return -1;
  1158.  
  1159.   switch (file_hdr->f_magic)
  1160.     {
  1161. #ifdef MC68MAGIC
  1162.     case MC68MAGIC:
  1163. #endif
  1164. #ifdef NS32GMAGIC
  1165.       case NS32GMAGIC:
  1166.       case NS32SMAGIC:
  1167. #endif
  1168. #ifdef I386MAGIC
  1169.     case I386MAGIC:
  1170. #endif
  1171. #ifdef CLIPPERMAGIC
  1172.     case CLIPPERMAGIC:
  1173. #endif
  1174. #if defined (MC68KWRMAGIC) \
  1175.   && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
  1176.     case MC68KWRMAGIC:
  1177. #endif
  1178. #ifdef MC68KROMAGIC
  1179.     case MC68KROMAGIC:
  1180.     case MC68KPGMAGIC:
  1181. #endif
  1182. #ifdef MC88DGMAGIC
  1183.     case MC88DGMAGIC:
  1184. #endif      
  1185. #ifdef MC88MAGIC
  1186.     case MC88MAGIC:
  1187. #endif      
  1188. #ifdef I960ROMAGIC
  1189.     case I960ROMAGIC:        /* Intel 960 */
  1190. #endif
  1191. #ifdef I960RWMAGIC
  1192.     case I960RWMAGIC:        /* Intel 960 */
  1193. #endif
  1194.     return file_hdr->f_nsyms;
  1195.  
  1196.       default:
  1197. #ifdef BADMAG
  1198.     if (BADMAG(file_hdr))
  1199.       return -1;
  1200.     else
  1201.       return file_hdr->f_nsyms;
  1202. #else
  1203.     return -1;
  1204. #endif
  1205.     }
  1206. }
  1207. #endif
  1208.  
  1209. /* Read the next symbol, swap it, and return it in both internal_syment
  1210.    form, and coff_symbol form.  Also return its first auxent, if any,
  1211.    in internal_auxent form, and skip any other auxents.  */
  1212.  
  1213. static void
  1214. read_one_sym (cs, sym, aux)
  1215.     register struct coff_symbol *cs;
  1216.     register struct internal_syment *sym;
  1217.     register union internal_auxent *aux;
  1218. {
  1219.   int i;
  1220.  
  1221.   cs->c_symnum = symnum;
  1222.   fread (temp_sym, local_symesz, 1, nlist_stream_global);
  1223.   bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *)sym);
  1224.   cs->c_naux = sym->n_numaux & 0xff;
  1225.   if (cs->c_naux >= 1)
  1226.     {
  1227.     fread (temp_aux, local_auxesz, 1, nlist_stream_global);
  1228.     bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
  1229.               (char *)aux);
  1230.     /* If more than one aux entry, read past it (only the first aux
  1231.        is important). */
  1232.     for (i = 1; i < cs->c_naux; i++)
  1233.       fread (temp_aux, local_auxesz, 1, nlist_stream_global);
  1234.     }
  1235.   cs->c_name = getsymname (sym);
  1236.   cs->c_value = sym->n_value;
  1237.   cs->c_sclass = (sym->n_sclass & 0xff);
  1238.   cs->c_secnum = sym->n_scnum;
  1239.   cs->c_type = (unsigned) sym->n_type;
  1240.   if (!SDB_TYPE (cs->c_type))
  1241.     cs->c_type = 0;
  1242.  
  1243.   symnum += 1 + cs->c_naux;
  1244. }
  1245.  
  1246. /* Support for string table handling */
  1247.  
  1248. static char *stringtab = NULL;
  1249.  
  1250. static int
  1251. init_stringtab (chan, offset)
  1252.     int chan;
  1253.     long offset;
  1254. {
  1255.   long length;
  1256.   int val;
  1257.   unsigned char lengthbuf[4];
  1258.  
  1259.   if (stringtab)
  1260.     {
  1261.       free (stringtab);
  1262.       stringtab = NULL;
  1263.     }
  1264.  
  1265.   if (lseek (chan, offset, 0) < 0)
  1266.     return -1;
  1267.  
  1268.   val = myread (chan, (char *)lengthbuf, sizeof lengthbuf);
  1269.   length = bfd_h_get_32 (symfile_bfd, lengthbuf);
  1270.  
  1271.   /* If no string table is needed, then the file may end immediately
  1272.      after the symbols.  Just return with `stringtab' set to null. */
  1273.   if (val != sizeof length || length < sizeof length)
  1274.     return 0;
  1275.  
  1276.   stringtab = (char *) xmalloc (length);
  1277.   if (stringtab == NULL)
  1278.     return -1;
  1279.  
  1280.   bcopy (&length, stringtab, sizeof length);
  1281.   if (length == sizeof length)        /* Empty table -- just the count */
  1282.     return 0;
  1283.  
  1284.   val = myread (chan, stringtab + sizeof length, length - sizeof length);
  1285.   if (val != length - sizeof length || stringtab[length - 1] != '\0')
  1286.     return -1;
  1287.  
  1288.   return 0;
  1289. }
  1290.  
  1291. static void
  1292. free_stringtab ()
  1293. {
  1294.   if (stringtab)
  1295.     free (stringtab);
  1296.   stringtab = NULL;
  1297. }
  1298.  
  1299. static char *
  1300. getsymname (symbol_entry)
  1301.     struct internal_syment *symbol_entry;
  1302. {
  1303.   static char buffer[SYMNMLEN+1];
  1304.   char *result;
  1305.  
  1306.   if (symbol_entry->_n._n_n._n_zeroes == 0)
  1307.     {
  1308.       result = stringtab + symbol_entry->_n._n_n._n_offset;
  1309.     }
  1310.   else
  1311.     {
  1312.       strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
  1313.       buffer[SYMNMLEN] = '\0';
  1314.       result = buffer;
  1315.     }
  1316.   return result;
  1317. }
  1318.  
  1319. static char *
  1320. getfilename (aux_entry)
  1321.     union internal_auxent *aux_entry;
  1322. {
  1323.   static char buffer[BUFSIZ];
  1324.   register char *temp;
  1325.   char *result;
  1326.  
  1327. #ifndef COFF_NO_LONG_FILE_NAMES
  1328. #if defined (x_zeroes)
  1329.   /* Data General.  */
  1330.   if (aux_entry->x_zeroes == 0)
  1331.     strcpy (buffer, stringtab + aux_entry->x_offset);
  1332. #else /* no x_zeroes */
  1333.   if (aux_entry->x_file.x_n.x_zeroes == 0)
  1334.     strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
  1335. #endif /* no x_zeroes */
  1336.   else
  1337. #endif /* COFF_NO_LONG_FILE_NAMES */
  1338.     {
  1339. #if defined (x_name)
  1340.       /* Data General.  */
  1341.       strncpy (buffer, aux_entry->x_name, FILNMLEN);
  1342. #else
  1343.       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
  1344. #endif
  1345.       buffer[FILNMLEN] = '\0';
  1346.     }
  1347.   result = buffer;
  1348.   if ((temp = strrchr (result, '/')) != NULL)
  1349.     result = temp + 1;
  1350.   return (result);
  1351. }
  1352.  
  1353. /* Support for line number handling */
  1354. static char *linetab = NULL;
  1355. static long linetab_offset;
  1356. static unsigned long linetab_size;
  1357.  
  1358. /* Read in all the line numbers for fast lookups later.  Leave them in
  1359.    external (unswapped) format in memory; we'll swap them as we enter
  1360.    them into GDB's data structures.  */
  1361.  
  1362. static int
  1363. init_lineno (chan, offset, size)
  1364.     int chan;
  1365.     long offset;
  1366.     int size;
  1367. {
  1368.   int val;
  1369.  
  1370.   linetab_offset = offset;
  1371.   linetab_size = size;
  1372.  
  1373.   if (size == 0)
  1374.     return 0;
  1375.  
  1376.   if (lseek (chan, offset, 0) < 0)
  1377.     return -1;
  1378.   
  1379.   /* Allocate the desired table, plus a sentinel */
  1380.   linetab = (char *) xmalloc (size + local_linesz);
  1381.  
  1382.   val = myread (chan, linetab, size);
  1383.   if (val != size)
  1384.     return -1;
  1385.  
  1386.   /* Terminate it with an all-zero sentinel record */
  1387.   bzero (linetab + size, local_linesz);
  1388.  
  1389.   make_cleanup (free, linetab);        /* Be sure it gets de-allocated. */
  1390.   return 0;
  1391. }
  1392.  
  1393. #if !defined (L_LNNO32)
  1394. #define L_LNNO32(lp) ((lp)->l_lnno)
  1395. #endif
  1396.  
  1397. static void
  1398. enter_linenos (file_offset, first_line, last_line)
  1399.     long file_offset;
  1400.     register int first_line;
  1401.     register int last_line;
  1402. {
  1403.   register char *rawptr;
  1404.   struct internal_lineno lptr;
  1405.  
  1406.   if (file_offset < linetab_offset)
  1407.     {
  1408.       complain (&lineno_complaint, (char *) file_offset);
  1409.       if (file_offset > linetab_size)    /* Too big to be an offset? */
  1410.     return;
  1411.       file_offset += linetab_offset;  /* Try reading at that linetab offset */
  1412.     }
  1413.   
  1414.   rawptr = &linetab[file_offset - linetab_offset];
  1415.  
  1416.   /* skip first line entry for each function */
  1417.   rawptr += local_linesz;
  1418.   /* line numbers start at one for the first line of the function */
  1419.   first_line--;
  1420.  
  1421.   for (;;) {
  1422.     bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
  1423.     rawptr += local_linesz;
  1424.     /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
  1425.     if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
  1426.       coff_record_line (first_line + L_LNNO32 (&lptr), lptr.l_addr.l_paddr);
  1427.     else
  1428.       break;
  1429.   } 
  1430. }
  1431.  
  1432. static void
  1433. patch_type (type, real_type)
  1434.     struct type *type;
  1435.     struct type *real_type;
  1436. {
  1437.   register struct type *target = TYPE_TARGET_TYPE (type);
  1438.   register struct type *real_target = TYPE_TARGET_TYPE (real_type);
  1439.   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
  1440.  
  1441.   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
  1442.   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
  1443.   TYPE_FIELDS (target) = (struct field *)
  1444.     obstack_alloc (¤t_objfile -> type_obstack, field_size);
  1445.  
  1446.   bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
  1447.  
  1448.   if (TYPE_NAME (real_target))
  1449.     {
  1450.       if (TYPE_NAME (target))
  1451.     free (TYPE_NAME (target));
  1452.       TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
  1453.     }
  1454. }
  1455.  
  1456. /* Patch up all appropriate typedef symbols in the opaque_type_chains
  1457.    so that they can be used to print out opaque data structures properly.  */
  1458.  
  1459. static void
  1460. patch_opaque_types (s)
  1461.      struct symtab *s;
  1462. {
  1463.   register struct block *b;
  1464.   register int i;
  1465.   register struct symbol *real_sym;
  1466.   
  1467.   /* Go through the per-file symbols only */
  1468.   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
  1469.   for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
  1470.     {
  1471.       /* Find completed typedefs to use to fix opaque ones.
  1472.      Remove syms from the chain when their types are stored,
  1473.      but search the whole chain, as there may be several syms
  1474.      from different files with the same name.  */
  1475.       real_sym = BLOCK_SYM (b, i);
  1476.       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
  1477.       SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
  1478.       TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
  1479.       TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
  1480.     {
  1481.       register char *name = SYMBOL_NAME (real_sym);
  1482.       register int hash = hashname (name);
  1483.       register struct symbol *sym, *prev;
  1484.       
  1485.       prev = 0;
  1486.       for (sym = opaque_type_chain[hash]; sym;)
  1487.         {
  1488.           if (name[0] == SYMBOL_NAME (sym)[0] &&
  1489.           !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
  1490.         {
  1491.           if (prev)
  1492.             {
  1493.               SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
  1494.             }
  1495.           else
  1496.             {
  1497.               opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
  1498.             }
  1499.           
  1500.           patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
  1501.           
  1502.           if (prev)
  1503.             {
  1504.               sym = SYMBOL_VALUE_CHAIN (prev);
  1505.             }
  1506.           else
  1507.             {
  1508.               sym = opaque_type_chain[hash];
  1509.             }
  1510.         }
  1511.           else
  1512.         {
  1513.           prev = sym;
  1514.           sym = SYMBOL_VALUE_CHAIN (sym);
  1515.         }
  1516.         }
  1517.     }
  1518.     }
  1519. }
  1520.  
  1521. static struct symbol *
  1522. process_coff_symbol (cs, aux, objfile)
  1523.      register struct coff_symbol *cs;
  1524.      register union internal_auxent *aux;
  1525.      struct objfile *objfile;
  1526. {
  1527.   register struct symbol *sym
  1528.     = (struct symbol *) obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
  1529.   char *name;
  1530. #ifdef NAMES_HAVE_UNDERSCORE
  1531.   int offset = 1;
  1532. #else
  1533.   int offset = 0;
  1534. #endif
  1535.   struct type *temptype;
  1536.  
  1537.   bzero (sym, sizeof (struct symbol));
  1538.   name = cs->c_name;
  1539.   name = (name[0] == '_' ? name + offset : name);
  1540.   SYMBOL_NAME (sym) = obstack_copy0 (&objfile->symbol_obstack, name, strlen (name));
  1541.  
  1542.   /* default assumptions */
  1543.   SYMBOL_VALUE (sym) = cs->c_value;
  1544.   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1545.  
  1546.   if (ISFCN (cs->c_type))
  1547.     {
  1548. #if 0
  1549.        /* FIXME:  This has NOT been tested.  The DBX version has.. */
  1550.        /* Generate a template for the type of this function.  The 
  1551.       types of the arguments will be added as we read the symbol 
  1552.       table. */
  1553.        struct type *new = (struct type *)
  1554.             obstack_alloc (&objfile->symbol_obstack, sizeof (struct type));
  1555.        
  1556.        bcopy(lookup_function_type (decode_function_type (cs, cs->c_type, aux)),
  1557.          new, sizeof(struct type));
  1558.        SYMBOL_TYPE (sym) = new;
  1559.        in_function_type = SYMBOL_TYPE(sym);
  1560. #else
  1561.        SYMBOL_TYPE(sym) = 
  1562.      lookup_function_type (decode_function_type (cs, cs->c_type, aux));
  1563. #endif
  1564.  
  1565.       SYMBOL_CLASS (sym) = LOC_BLOCK;
  1566.       if (cs->c_sclass == C_STAT)
  1567.     coff_add_symbol_to_list (sym, &coff_file_symbols);
  1568.       else if (cs->c_sclass == C_EXT)
  1569.     coff_add_symbol_to_list (sym, &coff_global_symbols);
  1570.     }
  1571.   else
  1572.     {
  1573.       SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
  1574.       switch (cs->c_sclass)
  1575.     {
  1576.       case C_NULL:
  1577.         break;
  1578.  
  1579.       case C_AUTO:
  1580.         SYMBOL_CLASS (sym) = LOC_LOCAL;
  1581.         coff_add_symbol_to_list (sym, &coff_local_symbols);
  1582.         break;
  1583.  
  1584.       case C_EXT:
  1585.         SYMBOL_CLASS (sym) = LOC_STATIC;
  1586.         SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1587.         coff_add_symbol_to_list (sym, &coff_global_symbols);
  1588.         break;
  1589.  
  1590.       case C_STAT:
  1591.         SYMBOL_CLASS (sym) = LOC_STATIC;
  1592.         SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1593.         if (within_function) {
  1594.           /* Static symbol of local scope */
  1595.           coff_add_symbol_to_list (sym, &coff_local_symbols);
  1596.         }
  1597.         else {
  1598.           /* Static symbol at top level of file */
  1599.           coff_add_symbol_to_list (sym, &coff_file_symbols);
  1600.         }
  1601.         break;
  1602.  
  1603. #ifdef C_GLBLREG        /* AMD coff */
  1604.       case C_GLBLREG:
  1605. #endif
  1606.       case C_REG:
  1607.         SYMBOL_CLASS (sym) = LOC_REGISTER;
  1608.         SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
  1609.         coff_add_symbol_to_list (sym, &coff_local_symbols);
  1610.         break;
  1611.  
  1612.       case C_LABEL:
  1613.         break;
  1614.  
  1615.       case C_ARG:
  1616.         SYMBOL_CLASS (sym) = LOC_ARG;
  1617. #if 0
  1618.         /* FIXME:  This has not been tested. */
  1619.         /* Add parameter to function.  */
  1620.         add_param_to_type(&in_function_type,sym);
  1621. #endif
  1622.         coff_add_symbol_to_list (sym, &coff_local_symbols);
  1623. #if !defined (BELIEVE_PCC_PROMOTION)
  1624.         /* If PCC says a parameter is a short or a char,
  1625.            it is really an int.  */
  1626.         temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
  1627.         if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
  1628.         && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
  1629.         {
  1630.             SYMBOL_TYPE (sym) = TYPE_UNSIGNED (SYMBOL_TYPE (sym))
  1631.             ? lookup_fundamental_type (current_objfile,
  1632.                            FT_UNSIGNED_INTEGER)
  1633.                 : temptype;
  1634.         }
  1635. #endif
  1636.         break;
  1637.  
  1638.       case C_REGPARM:
  1639.         SYMBOL_CLASS (sym) = LOC_REGPARM;
  1640.         SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
  1641.         coff_add_symbol_to_list (sym, &coff_local_symbols);
  1642. #if !defined (BELIEVE_PCC_PROMOTION)
  1643.         /* If PCC says a parameter is a short or a char,
  1644.            it is really an int.  */
  1645.         temptype = lookup_fundamental_type (current_objfile, FT_INTEGER);
  1646.         if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
  1647.         && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
  1648.         {
  1649.             SYMBOL_TYPE (sym) = TYPE_UNSIGNED (SYMBOL_TYPE (sym))
  1650.             ? lookup_fundamental_type (current_objfile,
  1651.                            FT_UNSIGNED_INTEGER)
  1652.                 : temptype;
  1653.         }
  1654. #endif
  1655.         break;
  1656.         
  1657.       case C_TPDEF:
  1658.         SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1659.         SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1660.  
  1661.         /* If type has no name, give it one */
  1662.         if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
  1663.           TYPE_NAME (SYMBOL_TYPE (sym)) = concat (SYMBOL_NAME (sym), NULL);
  1664.  
  1665.         /* Keep track of any type which points to empty structured type,
  1666.         so it can be filled from a definition from another file */
  1667.         if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
  1668.         TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0)
  1669.           {
  1670.         register int i = hashname (SYMBOL_NAME (sym));
  1671.  
  1672.         SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
  1673.         opaque_type_chain[i] = sym;
  1674.           }
  1675.         coff_add_symbol_to_list (sym, &coff_file_symbols);
  1676.         break;
  1677.  
  1678.       case C_STRTAG:
  1679.       case C_UNTAG:
  1680.       case C_ENTAG:
  1681.         SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1682.         SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1683.         if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
  1684.           TYPE_NAME (SYMBOL_TYPE (sym))
  1685.         = concat ("",
  1686.               (cs->c_sclass == C_ENTAG
  1687.                ? "enum "
  1688.                : (cs->c_sclass == C_STRTAG
  1689.                   ? "struct " : "union ")),
  1690.               SYMBOL_NAME (sym), NULL);
  1691.         coff_add_symbol_to_list (sym, &coff_file_symbols);
  1692.         break;
  1693.  
  1694.       default:
  1695.         break;
  1696.     }
  1697.     }
  1698.   return sym;
  1699. }
  1700.  
  1701. /* Decode a coff type specifier;
  1702.    return the type that is meant.  */
  1703.  
  1704. static
  1705. struct type *
  1706. decode_type (cs, c_type, aux)
  1707.      register struct coff_symbol *cs;
  1708.      unsigned int c_type;
  1709.      register union internal_auxent *aux;
  1710. {
  1711.   register struct type *type = 0;
  1712.   unsigned int new_c_type;
  1713.  
  1714.   if (c_type & ~N_BTMASK)
  1715.     {
  1716.       new_c_type = DECREF (c_type);
  1717.       if (ISPTR (c_type))
  1718.     {
  1719.       type = decode_type (cs, new_c_type, aux);
  1720.       type = lookup_pointer_type (type);
  1721.     }
  1722.       else if (ISFCN (c_type))
  1723.     {
  1724.       type = decode_type (cs, new_c_type, aux);
  1725.       type = lookup_function_type (type);
  1726.     }
  1727.       else if (ISARY (c_type))
  1728.     {
  1729.       int i, n;
  1730.       register unsigned short *dim;
  1731.       struct type *base_type;
  1732.  
  1733.       /* Define an array type.  */
  1734.       /* auxent refers to array, not base type */
  1735.       if (aux->x_sym.x_tagndx.l == 0)
  1736.         cs->c_naux = 0;
  1737.  
  1738.       /* shift the indices down */
  1739.       dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
  1740.       i = 1;
  1741.       n = dim[0];
  1742.       for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
  1743.         *dim = *(dim + 1);
  1744.       *dim = 0;
  1745.  
  1746.       type = (struct type *)
  1747.         obstack_alloc (¤t_objfile -> type_obstack,
  1748.                sizeof (struct type));
  1749.       bzero (type, sizeof (struct type));
  1750.       TYPE_OBJFILE (type) = current_objfile;
  1751.  
  1752.       base_type = decode_type (cs, new_c_type, aux);
  1753.  
  1754.       TYPE_CODE (type) = TYPE_CODE_ARRAY;
  1755.       TYPE_TARGET_TYPE (type) = base_type;
  1756.       TYPE_LENGTH (type) = n * TYPE_LENGTH (base_type);
  1757.     }
  1758.       return type;
  1759.     }
  1760.  
  1761.   /* Reference to existing type */
  1762.   if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
  1763.     {
  1764.       type = coff_alloc_type (aux->x_sym.x_tagndx.l);
  1765.       return type;
  1766.     }
  1767.  
  1768.   return decode_base_type (cs, BTYPE (c_type), aux);
  1769. }
  1770.  
  1771. /* Decode a coff type specifier for function definition;
  1772.    return the type that the function returns.  */
  1773.  
  1774. static
  1775. struct type *
  1776. decode_function_type (cs, c_type, aux)
  1777.      register struct coff_symbol *cs;
  1778.      unsigned int c_type;
  1779.      register union internal_auxent *aux;
  1780. {
  1781.   if (aux->x_sym.x_tagndx.l == 0)
  1782.     cs->c_naux = 0;    /* auxent refers to function, not base type */
  1783.  
  1784.   return decode_type (cs, DECREF (c_type), aux);
  1785. }
  1786.  
  1787. /* basic C types */
  1788.  
  1789. static
  1790. struct type *
  1791. decode_base_type (cs, c_type, aux)
  1792.      register struct coff_symbol *cs;
  1793.      unsigned int c_type;
  1794.      register union internal_auxent *aux;
  1795. {
  1796.   struct type *type;
  1797.  
  1798.   switch (c_type)
  1799.     {
  1800.       case T_NULL:
  1801.         /* shows up with "void (*foo)();" structure members */
  1802.     return lookup_fundamental_type (current_objfile, FT_VOID);
  1803.  
  1804. #if 0
  1805. /* DGUX actually defines both T_ARG and T_VOID to the same value.  */
  1806. #ifdef T_ARG
  1807.       case T_ARG:
  1808.     /* Shows up in DGUX, I think.  Not sure where.  */
  1809.     return lookup_fundamental_type (current_objfile, FT_VOID);    /* shouldn't show up here */
  1810. #endif
  1811. #endif /* 0 */
  1812.  
  1813. #ifdef T_VOID
  1814.       case T_VOID:
  1815.     /* Intel 960 COFF has this symbol and meaning.  */
  1816.     return lookup_fundamental_type (current_objfile, FT_VOID);
  1817. #endif
  1818.  
  1819.       case T_CHAR:
  1820.     return lookup_fundamental_type (current_objfile, FT_CHAR);
  1821.  
  1822.       case T_SHORT:
  1823.     return lookup_fundamental_type (current_objfile, FT_SHORT);
  1824.  
  1825.       case T_INT:
  1826.     return lookup_fundamental_type (current_objfile, FT_INTEGER);
  1827.  
  1828.       case T_LONG:
  1829.     return lookup_fundamental_type (current_objfile, FT_LONG);
  1830.  
  1831.       case T_FLOAT:
  1832.     return lookup_fundamental_type (current_objfile, FT_FLOAT);
  1833.  
  1834.       case T_DOUBLE:
  1835.     return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
  1836.  
  1837.       case T_STRUCT:
  1838.     if (cs->c_naux != 1)
  1839.       {
  1840.         /* anonymous structure type */
  1841.         type = coff_alloc_type (cs->c_symnum);
  1842.         TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1843.         TYPE_NAME (type) = concat ("struct ", "<opaque>", NULL);
  1844.         INIT_CPLUS_SPECIFIC(type);
  1845.         TYPE_LENGTH (type) = 0;
  1846.         TYPE_FIELDS (type) = 0;
  1847.         TYPE_NFIELDS (type) = 0;
  1848.       }
  1849.     else
  1850.       {
  1851.         type = coff_read_struct_type (cs->c_symnum,
  1852.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1853.                     aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
  1854.       }
  1855.     return type;
  1856.  
  1857.       case T_UNION:
  1858.     if (cs->c_naux != 1)
  1859.       {
  1860.         /* anonymous union type */
  1861.         type = coff_alloc_type (cs->c_symnum);
  1862.         TYPE_NAME (type) = concat ("union ", "<opaque>", NULL);
  1863.         INIT_CPLUS_SPECIFIC(type);
  1864.         TYPE_LENGTH (type) = 0;
  1865.         TYPE_LENGTH (type) = 0;
  1866.         TYPE_FIELDS (type) = 0;
  1867.         TYPE_NFIELDS (type) = 0;
  1868.       }
  1869.     else
  1870.       {
  1871.         type = coff_read_struct_type (cs->c_symnum,
  1872.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1873.                     aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
  1874.       }
  1875.     TYPE_CODE (type) = TYPE_CODE_UNION;
  1876.     return type;
  1877.  
  1878.       case T_ENUM:
  1879.     return coff_read_enum_type (cs->c_symnum,
  1880.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1881.                     aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
  1882.  
  1883.       case T_MOE:
  1884.     /* shouldn't show up here */
  1885.     break;
  1886.  
  1887.       case T_UCHAR:
  1888.     return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
  1889.  
  1890.       case T_USHORT:
  1891.     return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
  1892.  
  1893.       case T_UINT:
  1894.     return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
  1895.  
  1896.       case T_ULONG:
  1897.     return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
  1898.     }
  1899.   complain (&unexpected_type_complaint, cs->c_name);
  1900.   return lookup_fundamental_type (current_objfile, FT_VOID);
  1901. }
  1902.  
  1903. /* This page contains subroutines of read_type.  */
  1904.  
  1905. /* Read the description of a structure (or union type)
  1906.    and return an object describing the type.  */
  1907.  
  1908. static struct type *
  1909. coff_read_struct_type (index, length, lastsym)
  1910.      int index;
  1911.      int length;
  1912.      int lastsym;
  1913. {
  1914.   struct nextfield
  1915.     {
  1916.       struct nextfield *next;
  1917.       struct field field;
  1918.     };
  1919.  
  1920.   register struct type *type;
  1921.   register struct nextfield *list = 0;
  1922.   struct nextfield *new;
  1923.   int nfields = 0;
  1924.   register int n;
  1925.   char *name;
  1926. #ifdef NAMES_HAVE_UNDERSCORE
  1927.   int offset = 1;
  1928. #else
  1929.   int offset = 0;
  1930. #endif
  1931.   struct coff_symbol member_sym;
  1932.   register struct coff_symbol *ms = &member_sym;
  1933.   struct internal_syment sub_sym;
  1934.   union internal_auxent sub_aux;
  1935.   int done = 0;
  1936.  
  1937.   type = coff_alloc_type (index);
  1938.   TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1939.   INIT_CPLUS_SPECIFIC(type);
  1940.   TYPE_LENGTH (type) = length;
  1941.  
  1942.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  1943.     {
  1944.       read_one_sym (ms, &sub_sym, &sub_aux);
  1945.       name = ms->c_name;
  1946.       name = (name[0] == '_' ? name + offset : name);
  1947.  
  1948.       switch (ms->c_sclass)
  1949.     {
  1950.       case C_MOS:
  1951.       case C_MOU:
  1952.  
  1953.         /* Get space to record the next field's data.  */
  1954.         new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1955.         new->next = list;
  1956.         list = new;
  1957.  
  1958.         /* Save the data.  */
  1959.         list->field.name = savestring (name, strlen (name));
  1960.         list->field.type = decode_type (ms, ms->c_type, &sub_aux);
  1961.         list->field.bitpos = 8 * ms->c_value;
  1962.         list->field.bitsize = 0;
  1963.         nfields++;
  1964.         break;
  1965.  
  1966.       case C_FIELD:
  1967.  
  1968.         /* Get space to record the next field's data.  */
  1969.         new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1970.         new->next = list;
  1971.         list = new;
  1972.  
  1973.         /* Save the data.  */
  1974.         list->field.name = savestring (name, strlen (name));
  1975.         list->field.type = decode_type (ms, ms->c_type, &sub_aux);
  1976.         list->field.bitpos = ms->c_value;
  1977.         list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
  1978.         nfields++;
  1979.         break;
  1980.  
  1981.       case C_EOS:
  1982.         done = 1;
  1983.         break;
  1984.     }
  1985.     }
  1986.   /* Now create the vector of fields, and record how big it is.  */
  1987.  
  1988.   TYPE_NFIELDS (type) = nfields;
  1989.   TYPE_FIELDS (type) = (struct field *)
  1990.     obstack_alloc (¤t_objfile -> type_obstack,
  1991.            sizeof (struct field) * nfields);
  1992.  
  1993.   /* Copy the saved-up fields into the field vector.  */
  1994.  
  1995.   for (n = nfields; list; list = list->next)
  1996.     TYPE_FIELD (type, --n) = list->field;
  1997.  
  1998.   return type;
  1999. }
  2000.  
  2001. /* Read a definition of an enumeration type,
  2002.    and create and return a suitable type object.
  2003.    Also defines the symbols that represent the values of the type.  */
  2004. /* Currently assumes it's sizeof (int) and doesn't use length.  */
  2005.  
  2006. /* ARGSUSED */
  2007. static struct type *
  2008. coff_read_enum_type (index, length, lastsym)
  2009.      int index;
  2010.      int length;
  2011.      int lastsym;
  2012. {
  2013.   register struct symbol *sym;
  2014.   register struct type *type;
  2015.   int nsyms = 0;
  2016.   int done = 0;
  2017.   struct coff_pending **symlist;
  2018.   struct coff_symbol member_sym;
  2019.   register struct coff_symbol *ms = &member_sym;
  2020.   struct internal_syment sub_sym;
  2021.   union internal_auxent sub_aux;
  2022.   struct coff_pending *osyms, *syms;
  2023.   register int n;
  2024.   char *name;
  2025. #ifdef NAMES_HAVE_UNDERSCORE
  2026.   int offset = 1;
  2027. #else
  2028.   int offset = 0;
  2029. #endif
  2030.  
  2031.   type = coff_alloc_type (index);
  2032.   if (within_function)
  2033.     symlist = &coff_local_symbols;
  2034.   else
  2035.     symlist = &coff_file_symbols;
  2036.   osyms = *symlist;
  2037.  
  2038.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  2039.     {
  2040.       read_one_sym (ms, &sub_sym, &sub_aux);
  2041.       name = ms->c_name;
  2042.       name = (name[0] == '_' ? name + offset : name);
  2043.  
  2044.       switch (ms->c_sclass)
  2045.     {
  2046.       case C_MOE:
  2047.         sym = (struct symbol *) xmalloc (sizeof (struct symbol));
  2048.         bzero (sym, sizeof (struct symbol));
  2049.  
  2050.         SYMBOL_NAME (sym) = savestring (name, strlen (name));
  2051.         SYMBOL_CLASS (sym) = LOC_CONST;
  2052.         SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  2053.         SYMBOL_VALUE (sym) = ms->c_value;
  2054.         coff_add_symbol_to_list (sym, symlist);
  2055.         nsyms++;
  2056.         break;
  2057.  
  2058.       case C_EOS:
  2059.         /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
  2060.            up the count of how many symbols to read.  So stop
  2061.            on .eos.  */
  2062.         done = 1;
  2063.         break;
  2064.     }
  2065.     }
  2066.  
  2067.   /* Now fill in the fields of the type-structure.  */
  2068.  
  2069.   TYPE_LENGTH (type) =  TARGET_INT_BIT / TARGET_CHAR_BIT;
  2070.   TYPE_CODE (type) = TYPE_CODE_ENUM;
  2071.   TYPE_NFIELDS (type) = nsyms;
  2072.   TYPE_FIELDS (type) = (struct field *)
  2073.     obstack_alloc (¤t_objfile -> type_obstack,
  2074.            sizeof (struct field) * nsyms);
  2075.  
  2076.   /* Find the symbols for the values and put them into the type.
  2077.      The symbols can be found in the symlist that we put them on
  2078.      to cause them to be defined.  osyms contains the old value
  2079.      of that symlist; everything up to there was defined by us.  */
  2080.  
  2081.   for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
  2082.     {
  2083.       SYMBOL_TYPE (syms->symbol) = type;
  2084.       TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
  2085.       TYPE_FIELD_VALUE (type, n) = 0;
  2086.       TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (syms->symbol);
  2087.       TYPE_FIELD_BITSIZE (type, n) = 0;
  2088.     }
  2089.   /* Is this Modula-2's BOOLEAN type?  Flag it as such if so. */
  2090.   if(TYPE_NFIELDS(type) == 2 &&
  2091.      ((!strcmp(TYPE_FIELD_NAME(type,0),"TRUE") &&
  2092.        !strcmp(TYPE_FIELD_NAME(type,1),"FALSE")) ||
  2093.       (!strcmp(TYPE_FIELD_NAME(type,1),"TRUE") &&
  2094.        !strcmp(TYPE_FIELD_NAME(type,0),"FALSE"))))
  2095.      TYPE_CODE(type) = TYPE_CODE_BOOL;
  2096.   return type;
  2097. }
  2098.  
  2099. /* Register our ability to parse symbols for coff BFD files */
  2100.  
  2101. static struct sym_fns coff_sym_fns =
  2102. {
  2103.   "coff",        /* sym_name: name or name prefix of BFD target type */
  2104.   4,            /* sym_namelen: number of significant sym_name chars */
  2105.   coff_new_init,    /* sym_new_init: init anything gbl to entire symtab */
  2106.   coff_symfile_init,    /* sym_init: read initial info, setup for sym_read() */
  2107.   coff_symfile_read,    /* sym_read: read a symbol file into symtab */
  2108.   coff_symfile_finish,    /* sym_finish: finished with file, cleanup */
  2109.   NULL            /* next: pointer to next struct sym_fns */
  2110. };
  2111.  
  2112. void
  2113. _initialize_coffread ()
  2114. {
  2115.   add_symtab_fns(&coff_sym_fns);
  2116. }
  2117.